#! /usr/bin/env perl

###########################################################################
#
# vstat
#
###########################################################################
#
# This command interprets and regroups information on the status of files.  It can show which files are
# modified, outdated, unknown, etc.  With -v it will show which files are unchanged from the repository.  With
# -b it will tell you whether the files are on the trunk or a branch (and which branch).
#
# #########################################################################
#
# All the code herein is released under the Artistic License
#		( http://www.perl.com/language/misc/Artistic.html )
# Copyright (c) 2000-2011 Barefoot Software
#
###########################################################################

use strict;
use warnings;

use VCtools::Base;
use VCtools::Args;
use VCtools::Common;


#################################
# OPTIONS AND ENVIRONMENT VARS
#################################

VCtools::switch('recursive', 'r', 'operate recursively');
VCtools::switch('show_branches', 'b', 'show the branch name (or TRUNK if no branch) for all files');
VCtools::switch('top_branch', 'B', 'show the branch name for . only (similar to vbranch -s .)');
VCtools::switch('only_stat', 'o', 'only show files that match given status (modified|outdated|locked|unknown)', 'status');
VCtools::args('files', 'optlist', 'file(s) (or directory/ies) to show status of (default .)');
VCtools::getopts();

# remember, directories are files too
my @files = VCtools::files();
@files = (".") unless @files;
print STDERR "status for files: @files\n" if DEBUG >= 3;


#################################
# CHECK FOR ERRORS
#################################

if (VCtools::top_branch())
{
	# 	we have to show branches, because that's the point of the whole exercise
	# 	we don't care about outdated, and it's slower, so don't do that
	# 	just care about . so don't need recursive
	VCtools::cache_file_status( '.', { SHOW_BRANCHES => 1, NO_OUTDATED => 1, DONT_RECURSE => 1 } );

	VCtools::fatal_error("current directory is not in VC") unless VCtools::exists_in_vc('.');
}

my $proj = VCtools::verify_files_and_group(@files);


#################################
# MAIN
#################################

if (VCtools::top_branch())
{
	print "\nCURRENT BRANCH : ";
	VCtools::print_branch('.');
	print "\n";
}

my $retval = 0;
if (VCtools::only_stat())
{
	VCtools::cache_file_status(@files,
			{ SHOW_BRANCHES => VCtools::show_branches(), IGNORE_ERRORS => VCtools::ignore_errors() });
	my @stat_files = VCtools::get_all_with_status(VCtools::only_stat());
	if (@stat_files)
	{
		# no need to go further if we're just pretending
		exit 0 if VCtools::pretend();

		print join("\n", sort @stat_files), "\n";
	}
	else
	{
		$retval = 1;
	}
}
else
{
	$retval = VCtools::print_status($proj, @files, { SHOW_BRANCHES => VCtools::show_branches() });
}

exit $retval;							# should be 1 if any errors


#################################
# SUBS
#################################
